package com.stps.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stps.factory.ServiceFactory;
import com.stps.vo.TextBook;
import com.mysql.cj.util.StringUtils;

/**
 * Servlet implementation class TestJson
 */
@WebServlet("/TextBookQueryServlet")
public class TextBookQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TextBookQueryServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 取当前分页序号
		int pageIndex = Integer
				.parseInt(request.getParameter("pageIndex") == null ? "0" : request.getParameter("pageIndex"));
		// 取当前每页显示记录数
		int pageLines = Integer
				.parseInt(request.getParameter("pageLines") == null ? "0" : request.getParameter("pageLines"));
		String searchParams = request.getParameter("searchParams");
		String crud = request.getParameter("crud");
		TextBook TextBook = null;
		System.out.println("searchParams:" + searchParams);
		if (!StringUtils.isEmptyOrWhitespaceOnly(searchParams)) {
			TextBook = JSON.parseObject(searchParams, TextBook.class);
			TextBook.setId(0);
		}

		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");

		JSONObject jsonObj = new JSONObject();
		List<TextBook> data = null;
		try {
			Map<String, Object> map = ServiceFactory.getTextBookServiceFactory().list(pageIndex, pageLines, TextBook);
			data = (List<TextBook>) map.get("allTextBooks");
			jsonObj.put("code", 0);
			jsonObj.put("msg", "");
			jsonObj.put("count", map.get("TextBookCount"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		jsonObj.put("data", data);
		response.getWriter().append(jsonObj.toJSONString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
